package com.example.service;

import com.example.entity.*;
import com.example.mapper.SysMbtiQuestionMapper;
import com.example.mapper.SysResultMapper;
import com.example.mapper.SysStudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysMbtiQuestionService {

    @Resource
    private SysMbtiQuestionMapper sysMbtiQuestionMapper;
    @Resource
    private SysResultMapper sysResultMapper;
    @Resource
    private SysStudentMapper sysStudentMapper;


    public List<SysMbtiQuestion> getAllQuestions() {
        return sysMbtiQuestionMapper.findAll();
    }

    //分页查询
    public PageInfo<SysMbtiQuestion> selectPage(SysMbtiQuestion sysMbtiQuestion, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysMbtiQuestion> list = sysMbtiQuestionMapper.selectAll(sysMbtiQuestion);
        return PageInfo.of(list);
    }

    public void add(SysMbtiQuestion sysMbtiQuestion) {
        sysMbtiQuestionMapper.insert(sysMbtiQuestion);
    }

    public void update(SysMbtiQuestion sysMbtiQuestion) {
        sysMbtiQuestionMapper.update(sysMbtiQuestion);
    }

    public void delete(Integer id) {
        sysMbtiQuestionMapper.delete(id);
    }

    public Map<String, Object> submit(SysQuestion sysQuestion) {
        Map<String, Object> mapa = new HashMap<>();
        Map<String, Integer> mapt = new HashMap<>();
//        E 外向 I 内向 S 实感 N 直觉 T 思考 F 情感 J 判断 P 认知
        mapt.put("E", 0);
        mapt.put("I", 0);
        mapt.put("S", 0);
        mapt.put("N", 0);
        mapt.put("T", 0);
        mapt.put("F", 0);
        mapt.put("J", 0);
        mapt.put("P", 0);

        List<Integer> questionId = sysQuestion.getAnswers().stream().map(i -> i.getQuestionId()).collect(Collectors.toList());
        Map<Integer, String> maps = new HashMap<>();
        for (SysQuestion.QuestionList questionList : sysQuestion.getAnswers()) {
            maps.put(questionList.getQuestionId(), questionList.getChoice());
        }
        List<SysMbtiQuestion> list = sysMbtiQuestionMapper.selectIds(questionId);
        for (SysMbtiQuestion sysMbtiQuestion : list) {
            String string = maps.get(sysMbtiQuestion.getQuestionId());
            if(string.equals("A")){
                Integer integer = mapt.get(sysMbtiQuestion.getTypeA());
                integer ++;
                mapt.put(sysMbtiQuestion.getTypeA(), integer);
            }else if(string.equals("B")){
                Integer integer = mapt.get(sysMbtiQuestion.getTypeB());
                integer ++;
                mapt.put(sysMbtiQuestion.getTypeB(), integer);
            }
        }
        System.out.println(mapt);
        List<Map<String, Integer>> arrayList = new ArrayList<>();
        arrayList.add(mapt);
        mapa.put("json", arrayList);

//        int maxValue = Collections.max(mapt.values()); // 获取最大值
//        String maxKey = ""; // 初始化一个不存在的键作为默认值，以防找不到最大值的情况（理论上不应该发生）
//        for (Map.Entry<String, Integer> entry : mapt.entrySet()) {
//            if (entry.getValue() == maxValue) {
//                maxKey = entry.getKey(); // 找到对应的键
//                break; // 找到后退出循环
//            }
//        }
//        E 外向 I 内向 S 实感 N 直觉 T 思考 F 情感 J 判断 P 认知
        String maxKey = "";
        Integer e = mapt.get("E");
        Integer i = mapt.get("I");
        if(e >= i){
            maxKey += "E";
        }else {
            maxKey += "I";
        }
        Integer s = mapt.get("S");
        Integer n = mapt.get("N");
        if(s >= n){
            maxKey += "S";
        }else {
            maxKey += "N";
        }
        Integer t = mapt.get("T");
        Integer f = mapt.get("F");
        if(t >= f){
            maxKey += "T";
        }else {
            maxKey += "F";
        }
        Integer j = mapt.get("J");
        Integer p = mapt.get("P");
        if(j >= p){
            maxKey += "J";
        }else {
            maxKey += "P";
        }
        mapa.put("result", maxKey);
        SysStudent sysStudent = sysStudentMapper.selectById(sysQuestion.getUserId());
        SysResults sysResults = new SysResults();
        sysResults.setResult(maxKey);
        sysResults.setStudentId(sysStudent.getStudentId());
        sysResults.setStudentName(sysStudent.getStudentName());
        sysResults.setStudentClass(sysStudent.getStudentClass());
        sysResults.setStudentMajor(sysStudent.getStudentMajor());
        sysResults.setStudentNumber(sysStudent.getStudentNumber());
        sysResults.setStudentSchool(sysStudent.getStudentSchool());
        sysResultMapper.insert(sysResults);
        return mapa;
    }
}
